package com.ms.game.bank.db;

import com.ms.game.bank.message.BankResponse;
import com.ms.game.lobby.GameLobbyModule;
import com.ms.game.password.db.PasswordDisableTime;
import com.ms.game.password.db.PasswordErrorWay;
import com.ms.game.password.db.PasswordLogModel;
import com.ms.player.Player;
import com.ms.service.error.ErrorCode;
import com.ms.service.module.account.Account;
import com.ms.service.module.account.db.AccountDAO;
import com.ms.service.module.datacenterlite.CentralAsyncCommand;
import com.xpec.c4.db.dao.PlayerDAO;
import com.xpec.c4.db.model.PlayerModel;
import com.xpec.c4.game.time.Stamper;
import com.xpec.c4.game.time.TimeSys;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.game.utility.GUIDFactory;
import com.xpec.c4.game.utility.GUIDType;
import com.xpec.c4.service.message.MessageAction;

public class checkAndChangePasswordDCLAsync extends CentralAsyncCommand {

	private static final long serialVersionUID = 3481780407358099158L;

	/**
	 * 現在修改的密碼
	 */
	public String password;
	
	public String prePassword;
	
	public GUID accountID;
	
	public GUID playerID;
	
	public String playName;
	
	public PasswordErrorWay errorWay;
	
	private String bankPassword;
	
	/** 錯誤次數 */
	private int errorTimes;
	
	/** 不能在輸入時間 */
	private long disableTimes;
	
	private boolean isSave;

	/** 是否機器人 */
	private boolean robot;

	/** 解密的密碼*/
	private String reductionPassword ;
	
	/** 忘記密碼重置 **/
	private boolean forgotten = false;
	
	@Override
	public void asyncExecute() throws Exception {
		
		Account accountData = AccountDAO.loadAccountByAccountId(accountID);
		
		reductionPassword = Stamper.getInstance().Reduction(accountData.getBankPassword());
		if(reductionPassword == null){
			//解密錯誤
			return;
		}
		
		bankPassword = reductionPassword;

		if(accountData.getBankPassword() == "" || accountData.getBankPassword() == null){
			//密碼還未建立
			return;
		}
		
		PlayerModel model = PlayerDAO.getErrorTimesAndDisableTimeByGuid(playerID);
		disableTimes = model.getDisableTime();
		errorTimes = model.getErrorTimes();
		
		if(disableTimes > TimeSys.currentTimeMills()){
			return;
		}
		
		if (!forgotten) {
			if(!bankPassword.equals(prePassword)){
				//輸入錯誤紀錄
				model.setErrorTimes(errorTimes + 1);
				if(model.getErrorTimes() == 5){
					model.setDisableTime(TimeSys.currentTimeMills() + PasswordDisableTime.BANK_FIVE_TIMES_DISABLE_TIME);
				}else if(model.getErrorTimes() >= 10 && (model.getErrorTimes()%5)==0){
					model.setDisableTime(TimeSys.currentTimeMills() + PasswordDisableTime.BANK_TEN_TIMES_DISABLE_TIME);
				}
				
				PasswordLogModel log = new PasswordLogModel();
				log.setOrderId(GUIDFactory.createUUID(GUIDType.TYPE_BANK_MONEY).toString());
				log.setAccountId(accountID.toString());
				log.setPlayerId(playerID.toString());
				log.setPlayerName(playName);
				log.setPasswordEnter(password);
				log.setErrorTimes(model.getErrorTimes());
				log.setChangeWay(errorWay.getId());
				log.setChangeWayDesc(errorWay.getDesc());
				getCurrentModuleServer().saveDbLog(log);
				
				PlayerDAO.updatePlayerModelPasswordError(model);
				//密碼錯誤
				return;
			}else{
				//輸入正確歸0
				model.setErrorTimes(0);
				model.setDisableTime(0);
				model.setErrorTimesGift(0);
				model.setDisableTimeGift(0);
				PlayerDAO.updatePlayerModelPasswordError(model);
			}
		}
		
		// 把使用者密碼加密
		String encryptPassword = Stamper.getInstance().Save(password);
		isSave = BankDAO.UpdateBankPassword(accountID, encryptPassword);
		if (isSave) {
			password = encryptPassword;
		}
		
	}

	@Override
	public void callBackExecute(Object... userData) throws Exception {
		
		GameLobbyModule module = (GameLobbyModule)userData[0];
		int messageType = (int)userData[1];
		GUID sessionID = (GUID)userData[2];
		
		BankResponse response = new BankResponse(); 
		
		if(reductionPassword == null){
			response.errorCode = ErrorCode.DECRYPTION_FAILED;
			MessageAction.sendMessage(sessionID, messageType, response);
			return;
		}
		
		if(bankPassword == "" || bankPassword == null){
			//密碼還未建立
			response.errorCode = ErrorCode.PASSWORD_CREATE_NOT_YET;
			MessageAction.sendMessage(sessionID, messageType, response);
			return;
		}
		
		if(!forgotten && !bankPassword.equals(prePassword)){
			
			Player playerSave= module.getZone().getPlayer(playerID);
			//如果在線上
			if(playerSave != null){
				//寫回zone player
				playerSave.setErrorTimes(errorTimes);
				playerSave.setDisableTime(disableTimes);
			}
			
			//密碼錯誤
			response.errorCode = ErrorCode.CHANGE_PASSWORD_ERROR;
			response.errorTimes = errorTimes + 1;
			MessageAction.sendMessage(sessionID, messageType, response);
			return;
		}
		
		if(!isSave){
			response.errorCode = ErrorCode.DB_OPERATION_FAIL;
			MessageAction.sendMessage(sessionID, messageType, response);
			return;
		}

		response.errorCode = ErrorCode.SUCCESS;
		MessageAction.sendMessage(sessionID, messageType, response);

	}

	public void setRobot(boolean robot) {
		this.robot = robot;
	}

	public boolean isForgotten() {
		return forgotten;
	}

	public void setForgotten(boolean forgotten) {
		this.forgotten = forgotten;
	}

}
